package cn.v2tila.rsosa.management.controller;

import cn.v2tila.rsosa.management.entity.MgtUserEntity;
import cn.v2tila.rsosa.management.entity.UserEntity;
import cn.v2tila.rsosa.management.service.MgtUserService;
import cn.v2tila.rsosa.management.service.UserService;
import cn.v2tila.rsosa.management.utils.IStatusMessage;
import cn.v2tila.rsosa.management.utils.PageDataResult;
import cn.v2tila.rsosa.management.vo.ResponseResult;
import cn.v2tila.rsosa.management.vo.ResultVO;
import cn.v2tila.rsosa.management.vo.User;
import cn.v2tila.rsosa.management.vo.UserDTO;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.codec.digest.DigestUtils;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * userController
 *
 * @author v2tila on 15:44 2019/2/7
 */

@Controller
@RequestMapping(value = "user")
public class userController {

    private static final Logger logger = LoggerFactory
            .getLogger(userController.class);

    @Autowired
    private MgtUserService mgtUserService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "userInfo")
    public String toUserList() {
        return "user/userInfo";
    }


    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public ResultVO login(UserDTO userDTO, HttpServletRequest request){
        ResultVO resultVO = new ResultVO();
        HttpSession session = request.getSession();
        MgtUserEntity mgtUserEntity = mgtUserService.findByUsername(userDTO.getUsername());
        if(mgtUserEntity==null){
            resultVO.setCode(0);
            resultVO.setMsg("该用户不存在");
        }else{
            if(!userDTO.getPassword().equals(mgtUserEntity.getPassword())){
                resultVO.setCode(0);
                resultVO.setMsg("密码错误");
            }else{
                User user = new User();
                user.setUsername(userDTO.getUsername());
                session.setAttribute("user", user);
                resultVO.setCode(1);
                resultVO.setMsg("登陆成功");

            }
        }
        return resultVO;

    }

    @GetMapping(value = "getMgtUsers")
    @ResponseBody
    public PageDataResult getMgtUsers(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   MgtUserEntity mgtUserEntity){
        if(mgtUserEntity.getUsername().equals("")){
            mgtUserEntity.setUsername(null);
        }
        return mgtUserService.getMgtUsers(page, limit,mgtUserEntity);

    }

    @GetMapping(value = "getUsers")
    @ResponseBody
    public PageDataResult getUsers(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   UserEntity userEntity){
        if(userEntity.getUniqueId().equals("")){
            userEntity.setUniqueId(null);
        }

        return userService.getUsers(page, limit,userEntity);
    }

    @RequestMapping(value = "findAll")
    @ResponseBody
    public List<MgtUserEntity> findAll() {
        return mgtUserService.findAll();
    }
    protected boolean validatorRequestParam(Object obj, ResponseResult response) {
        boolean flag = false;
        Validator validator = new Validator();
        List<ConstraintViolation> ret = validator.validate(obj);
        if (ret.size() > 0) {
            // 校验参数有误
            response.setCode(IStatusMessage.SystemStatus.PARAM_ERROR.getCode());
            response.setMessage(ret.get(0).getMessageTemplate());
        } else {
            flag = true;
        }
        return flag;
    }
}
